package leecode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
    682. 棒球比赛
    easy
 */
public class _682 {
    public static int calPoints(String[] operations) {
        List<Integer> res = new ArrayList<>();
        //解析
        for (int i = 0; i < operations.length; i++) {
            String op = operations[i];
            Integer lastNum;
            switch (op) {
                case "+" :
                    lastNum = res.get(res.size() - 1);
                    Integer lastTwoNum = res.get(res.size() - 2);
                    res.add(lastTwoNum + lastNum);
                    break;
                case "D" :
                    lastNum = res.get(res.size() - 1);
                    res.add(lastNum * 2);
                    break;
                case "C" :
                    res.remove(res.size() - 1);
                    break;
                default :
                    res.add(Integer.parseInt(op));
            }
        }
        //累计
        int num = 0;
        for (Integer re : res) {
            if (null != re) {
                num += re;
            }
        }
        return num;
    }

    /**
     * 找出最后一个数的下标，从指定位置开始找。
     * @param list 数组
     * @param index 指定下标
     * @return 目标下标
     */
    private static int findLastNumIndex(Integer[] list, int index) {
        //校验入参
        if (index >= list.length || index < 0) {
            System.err.println("入参有误！");
        } else {
            for (int i = index; i >= 0; i--) {
                Integer item = list[i];
                if (null != item) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        String[] params = new String[]{"5","2","C","D","+"};
        int a = calPoints(params);
        System.out.println(a);

    }
}
